# -*- coding: utf-8 -*-

from urllib import unquote
from google.appengine.ext import db
from google.appengine.ext import search

#MAX_LENGTH = 5
DISK_TYPES = set(['QQ', 'Foxmail', 'QQ FTN5K', 'Foxmail FTN5K', 'QQ+FTN5K', 'Foxmail+FTN5K', 'RayFile', 'JSharer',
                  'Namipan', 'SkyDrive', 'MegaUpload', 'ifile', '115', 'other'])


def decode(string):
    return unicode(unquote(string), 'utf8', 'ignore')

def checkLength(value):
    if value is None:
        return
    if not isinstancetype(value, list):
        raise TypeError, 'not a list'
    if len(value) > MAX_LENGTH:
        raise ValueError, 'length is too long'
    return value


class User(db.Model):
    user = db.UserProperty(required=True)
    name = db.StringProperty(required=True)
    register_date = db.DateTimeProperty(required=True, auto_now_add=True)
    money = db.IntegerProperty(default=0)
    rating = db.IntegerProperty(default=0)
    classification = db.IntegerProperty(required=True, choices=set([0, 1, 2]), indexed=False) # 0: G, 1: R15, 2: R18
    qq_line = db.StringProperty(indexed=False)
    fm_line = db.StringProperty(indexed=False)


class Resource(search.SearchableModel):
    title = db.StringProperty(required=True, indexed=False)
    author = db.ReferenceProperty(required=True, reference_class=User)
    #categories = db.StringListProperty(required=True, choices=set(['anime', 'comic', 'game', 'music', 'drama', 'picture']))
    categories = db.StringProperty(required=True, indexed=False)
    classification = db.IntegerProperty(required=True, choices=set([0, 1, 2])) # 0: G, 1: R15, 2: R18
    publish_time = db.DateTimeProperty(required=True, auto_now_add=True)
    #update_time = db.DateTimeProperty()
    expire_time = db.DateTimeProperty(required=True)
    search_fields = db.StringProperty(indexed=False)
    #search_fields = db.StringListProperty(validator=checkLength)
    season = db.IntegerProperty()
    vol = db.IntegerProperty()
    disk_type = db.StringProperty(required=True, choices=DISK_TYPES)
    description = db.TextProperty()
    total_size = db.StringProperty(indexed=False)
    price = db.IntegerProperty(default=0)
    rating = db.IntegerProperty(default=0)
    view = db.IntegerProperty(default=0)
    
    @classmethod
    def SearchableProperties(cls):
        return [['title', 'search_fields'], 'categories']


class DownloadItem(db.Model):
    resource = db.ReferenceProperty(required=True, reference_class=Resource)
    disk_type = db.StringProperty(required=True, choices=DISK_TYPES)
    size = db.StringProperty(indexed=False)
    download_link = db.LinkProperty(indexed=False)
    direct_link = db.LinkProperty(indexed=False)
    access_code = db.StringProperty(indexed=False)
    ftn5k = db.StringProperty(indexed=False)


class Vote(db.Model):
    resource = db.ReferenceProperty(required=True, reference_class=Resource)
    voter = db.ReferenceProperty(required=True, reference_class=User)
    rating = db.IntegerProperty(required=True, indexed=False)
    comment = db.TextProperty()
    